package com.cdn.allmobilestore.ui;

import java.util.Iterator;
import java.util.List;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.model.AppInfo;
import com.cdn.allmobilestore.ulti.DownloadInstallManager.Progress;
import com.cdn.allmobilestore.ulti.DownloadInstallManager;
import com.cdn.allmobilestore.ulti.LocalAppManager;
import com.cdn.allmobilestore.ulti.Log;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

public class ActionBottomButton extends ActionArea {
	private TextView mPrimary = new TextView(this.mContext);
	/**Phong
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public ActionBottomButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	/**Phong
	 * @param context
	 * @param attrs
	 */
	public ActionBottomButton(Context context, AttributeSet attrs) {
		super(context, attrs);		
		this.mPrimary.setTextAppearance(getContext(),R.style.TextAppearance_Button_Action_Large);
	    this.mPrimary.setDuplicateParentStateEnabled(true);	    
	    addView(this.mPrimary);
	    
	}

	public ActionBottomButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void bindInstalled(AppInfo appInfo) {	
		
		if(LocalAppManager.getManager().isInstalled(appInfo.getPackageName()))
		{
			Intent intent = this.mContext.getPackageManager().getLaunchIntentForPackage(appInfo.getPackageName());
		    if (intent == null)
		    {
		      InputMethodManager input = (InputMethodManager)this.mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		      List<InputMethodInfo> localList = input.getInputMethodList();
		      
		      if ((localList != null) && (localList.size() > 0))
		      {
		        Iterator<InputMethodInfo> method = localList.iterator();
		        while (method.hasNext())
		        {
		          InputMethodInfo inputMethod = (InputMethodInfo)method.next();
		          if ((inputMethod != null) && (!TextUtils.isEmpty(inputMethod.getSettingsActivity())) && (TextUtils.equals(appInfo.getPackageName(), inputMethod.getPackageName())))
		          {
		            intent = new Intent("android.intent.action.MAIN");
		            intent.setClassName(inputMethod.getPackageName(), inputMethod.getSettingsActivity());
		          }
		        }
		      }
		    }
		    if (intent == null)
		    {
		      setEnabled(false);
		      this.mPrimary.setText(getContext().getString(R.string.title_launch));	      
		    }
		    else
		    {
		      setEnabled(true);
		      this.mLaunchClickListener.setIntent(intent);
		      setOnClickListener(this.mLaunchClickListener);
		      this.mPrimary.setText(getContext().getString(R.string.title_launch));	    
		    }
		}
		else
			bindNormal(appInfo);
		
		    
	}

	@Override
	protected void bindInstalling(AppInfo appInfo) {
		if(DownloadInstallManager.getManager().mInstallManuallyMap.containsKey(appInfo.getPackageName()))
		{
			setEnabled(true);
	
		}
		else
		{
			setEnabled(false);
		    this.mPrimary.setTextAppearance(getContext(), R.style.TextAppearance_Button_Action_Large);
		}
		

	}

	@Override
	protected void bindNormal(AppInfo appInfo) {
		this.iClicked = false;
		setEnabled(true);
	    this.mPrimary.setText(getContext().getString(R.string.title_install));
	    this.mPrimary.setTextAppearance(getContext(),  R.style.TextAppearance_Button_Action_Large);	    
	    setOnClickListener(this.mArrangeClickListener);
	}

	@Override
	protected void bindPrice(AppInfo appInfo) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void bindUpdate(AppInfo appInfo) {
		setEnabled(true);
		this.iClicked = false;
	    this.mPrimary.setText(getContext().getString(R.string.title_update));
	    this.mPrimary.setTextAppearance(getContext(), R.style.TextAppearance_Button_Action_Large);
	    setOnClickListener(this.mArrangeClickListener);

	}

	@Override
	protected void updateProgressConnecting(AppInfo appInfo, Progress progress) {		
		this.mPrimary.setText(getContext().getString(R.string.title_connecting));
	}

	@Override
	protected void updateProgressDownloading(AppInfo appInfo, Progress progress) {
		String str = "0";
		if ((progress.totalBytes > 0) && (progress.currBytes > 0))
	     str = String.valueOf(Math.round(100.0D * progress.currBytes / progress.totalBytes));		    
	    this.mPrimary.setText(getContext().getString(R.string.title_downloadx, str));
	}

	@Override
	protected void updateProgressInstalling(AppInfo appInfo, Progress progress) {
		if(DownloadInstallManager.getManager().mInstallManuallyMap.containsKey(appInfo.getPackageName()))
		{
			this.mPrimary.setText(getContext().getString(R.string.text_setup_install));
		}
		else
			this.mPrimary.setText(getContext().getString(R.string.title_installing));
	}

	@Override
	protected void updateProgressPaused(AppInfo appInfo, Progress progress) {
		setEnabled(true);
		this.mPrimary.setText(getContext().getString(R.string.title_pause));
	}

	@Override
	protected void updateProgressPending(AppInfo appInfo, Progress progress) {
		this.mPrimary.setText(getContext().getString(R.string.title_pending));
	}

	@Override
	protected void updateProgressVerifying(AppInfo appInfo, Progress progress) {
		this.mPrimary.setText(getContext().getString(R.string.title_verifying)); 
	}

	@Override
	protected void updateProgressInstalled(AppInfo appInfo, Progress progress) {
		// TODO Auto-generated method stub
		bindInstalled(appInfo);
	}
}
